//
//  ReachabilityManager.m
//  RoadCast
//
//  Created by Nitish Makhija on 27/10/16.

#import "ReachabilityManager.h"
#import "RCBaseReachability.h"

static NSString *kHostAddress = @"www.baidu.com";

@interface ReachabilityManager ()

@property (nonatomic) RCBaseReachability *hostReachability;
@property (nonatomic) RCBaseReachability *internetReachability;
@property (nonatomic) ReachabilityBlock reachabilityBlock;

@end

@implementation ReachabilityManager

- (instancetype)init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
        
        self.hostReachability = [RCBaseReachability reachabilityWithHostName:kHostAddress];
        [self.hostReachability startNotifier];
        
        self.internetReachability = [RCBaseReachability reachabilityForInternetConnection];
        [self.internetReachability startNotifier];
    }
    return self;
}

+ (instancetype)sharedManager {
    static dispatch_once_t onceToken;
    static ReachabilityManager *sharedManager = nil;
    dispatch_once(&onceToken, ^{
        sharedManager = [[[self class] alloc] init];
    });
    
    return sharedManager;
}

-(void)startReachability:(ReachabilityBlock)block {
    self.reachabilityBlock = block;
}

- (void)reachabilityChanged:(NSNotification *)notification {
    RCBaseReachability *currentReachablity = [notification object];

    if (currentReachablity == self.hostReachability) {
        //host is unreachable
        //internet is not available
        NetworkStatus internetStatus = [currentReachablity currentReachabilityStatus];
        
        switch (internetStatus) {
            case NotReachable:
                //change the online status to no here
                NSLog(@"No internet available");
                if (self.reachabilityBlock) {
                    self.reachabilityBlock(NO);
                }
                break;
                
            case ReachableViaWWAN:
                if (![currentReachablity connectionRequired]) {
                    //change the online status to yes
                    NSLog(@"YES internet available");
                    if (self.reachabilityBlock) {
                        self.reachabilityBlock(YES);
                    }
                }
                break;
                
            case ReachableViaWiFi:
                //change the status to yes
                NSLog(@"YES internet available");
                if (self.reachabilityBlock) {
                    self.reachabilityBlock(YES);
                }
                break;
                
            default:
                break;
        }

    }
    if (currentReachablity == self.internetReachability) {
        // checking the internet connectivity
        // just checks wheather the device is connected to a routing interface
        // host reachablity can tell the same thing
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
    [self.hostReachability stopNotifier];
    [self.internetReachability stopNotifier];
}


@end
